/*
    This file is part of QTau
    Copyright (C) 2013-2018  Tobias "Tomoko" Platen <tplaten@posteo.de>
    Copyright (C) 2013       digited       <https://github.com/digited>
    Copyright (C) 2010-2013  HAL@ShurabaP  <https://github.com/haruneko>

    QTau is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    SPDX-License-Identifier: GPL-3.0+
*/
#ifndef ECANTORIX_SYNTH_H
#define ECANTORIX_SYNTH_H



#include "PluginInterfaces.h"
#include <QJsonArray>
#include <QList>
#include <QMap>
#include "sekai/VVDReader.h"
#include "jack/ringbuffer.h"
#include <sinsy/ringbuffer.h>

#include "wavtool.h"

#define VOWEL 0
#define VCONSONANT 1
#define HCONSONANT 2
#define NASAL 3
#define LIQUID 4


struct phoType
{
    int defaultLength;
    int voxtype;
};

struct phoEvent
{
    phoType type;
    QString symbol;
};

struct eCantorixNote
{
    float vstart;
    float rlength;
    float f0;
    float preutter;
    QList<phoEvent> eventList;
    QString mbrolaPho;
};

class eCantorixSynth : public QObject, public ISynth
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID c_isynth_comname FILE "ecantorix_synth.json")
    Q_INTERFACES(ISynth)
    int _jack_samplerate=0;
    QString _cacheDir;
    QString _voiceName;
    QMap<QString,QString> _voices;
    QString _internalVoice;
    QJsonArray _score;
    IController* _ctrl=nullptr;
    RingBuffer* _rb=nullptr;
    QMap<QString,phoType> _phoTypes;
    QList<phoEvent> _eventList;
    QList<eCantorixNote> _notes;
    WavTool _wavtool;


public:
    eCantorixSynth() {}

    //plugin loading
    QString name()                              override;
    QString description()                       override;
    QString version()                           override;
    void setup(IController *ctrl)               override;

    //synth base
    bool setCacheDir(QString cacheDir)          override;
    bool synthesize(IScore* score)              override;
    bool synthIsRealtime()                      override;
    int readData(float *data, int size)         override;

    //lyricizer
    QString getTranscription(QString txt)       override;
    bool doPhonemeTransformation(QStringList& lyrics) override;

    //voice manager
    bool setVoice(QString voiceName) override;
    QStringList listVoices() override;



private:
    void lyricize(QString lyric);
    void lookupPho(QString pho);
    float getPreutter();
signals:
    void signalStatus(int status);
    void logError(QString error);
    void logSuccess(QString success);
    void logDebug(QString debug);

public slots:
   // void synthprocess_finished(int exitCode, QProcess::ExitStatus exitStatus);
    void on_logError(QString error);
    void on_logSuccess(QString success);
    void on_logDebug(QString debug);

};

#define SETSEEK 1
#endif // LESYNTH_H
